package com.applancer.fragment;

import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.applancer.model.FeedModel;
import com.applancer.nativecmsflip.R;

public class FeedDetailFragment extends Fragment {

	protected WebView webview;
	protected View contentView;
	protected FeedModel model;

	public FeedDetailFragment(FeedModel fm) {
		model = fm;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		contentView = inflater.inflate(R.layout.feed_detail_fragment, null);
		return contentView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initial();
		execution();

	}

	private void initial() {
		webview = (WebView) contentView.findViewById(R.id.webview_id);

	}

	private void execution() {

		webview.setWebViewClient(new MyBrowser());
		webview.getSettings().setLoadsImagesAutomatically(true);

		new AsynchronoulyWebview().execute(model.getContent());
	}

	public class AsynchronoulyWebview extends AsyncTask<String, String, String> {

		@Override
		protected String doInBackground(String... params) {

			String htmlContent = "<html>"
					+ "<style type=\"text/css\">  "
					+ "img {  width: 100%;height:auto;}  iframe {width: 100%;height:auto;}"
					+ "</style>" + "<body >" + params[0] + "</body>"
					+ "</html>";

			htmlContent = htmlContent.replace("&amp;#8217;", "'");
			htmlContent = htmlContent.replace("style=\"width:", "");

			return htmlContent;
		}

		protected void onProgressUpdate(String... progress) {
		}

		@SuppressLint("NewApi")
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			webview.loadDataWithBaseURL(null, result, "text/html", "UTF-8",
					null);
			int currentapiVersion = android.os.Build.VERSION.SDK_INT;
			if (currentapiVersion >= 19) {
				webview.getSettings().setLoadWithOverviewMode(true);
				webview.getSettings().setUseWideViewPort(false);
				webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
				webview.setScrollbarFadingEnabled(true);
				webview.getSettings().setLayoutAlgorithm(
						LayoutAlgorithm.TEXT_AUTOSIZING);
			} else {
				webview.getSettings().setLayoutAlgorithm(
						LayoutAlgorithm.SINGLE_COLUMN);
			}
		}

	}

	private class MyBrowser extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {

			return true;

		}

	}

	// @SuppressLint("NewApi")
	// private void loadingContent(WebView wv) {
	//
	// String htmlContent = "<html>"
	// + "<style type=\"text/css\">  "
	// + "img {  width: 100%;height:auto;}  iframe {width: 100%;height:auto;}"
	// + "</style>" + "<body >" + model.getContent() + "</body>"
	// + "</html>";
	//
	// htmlContent = htmlContent.replace("&amp;#8217;", "'");
	// htmlContent = htmlContent.replace("style=\"width:", "");
	//
	// wv.loadDataWithBaseURL(null, htmlContent, "text/html", "UTF-8", null);
	// int currentapiVersion = android.os.Build.VERSION.SDK_INT;
	// if (currentapiVersion >= 19) {
	// wv.getSettings().setLoadWithOverviewMode(true);
	// wv.getSettings().setUseWideViewPort(false);
	// wv.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
	// wv.setScrollbarFadingEnabled(true);
	// wv.getSettings()
	// .setLayoutAlgorithm(LayoutAlgorithm.TEXT_AUTOSIZING);
	// } else {
	// wv.getSettings().setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
	// }
	// }
}
